package cn.seocoo.platform.api.controller;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.car.dto.CarDto;
import cn.seocoo.platform.common.CommonServiceImpl;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.contrast.service.ICarBrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: ApiBrowseController
 * @description: 浏览记录
 * @author: wujian
 * @Date: 2020-01-02 16:46
 **/
@RequestMapping("api/browse")
@RestController
public class ApiBrowseController {
    @Autowired
    private ICarBrowseService iCarBrowseService;


    @Autowired
    private CommonServiceImpl commonService;


    /**
     * @return cn.seocoo.platform.api.util.JSONResult
     * @Author WuJian
     * @Description //收藏列表
     * @Date 18:22 2020/1/2
     * @Param [appUserId]
     **/
    @GetMapping("/getBrowse")
    public JSONResult getBrowse(Integer appUserId) {
        try {
            List<CarDto> carlist = iCarBrowseService.getCarList(appUserId);
            commonService.getCarListForServcie(carlist);
            return JSONResult.ok(carlist);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }


    /**
     * @return cn.seocoo.platform.api.util.JSONResult
     * @Author WuJian
     * @Description //清空
     * @Date 17:23 2020/1/2
     * @Param [appUserId]
     **/
    @GetMapping("/deleteBrowse")
    public JSONResult deleteBrowse(Integer appUserId) {
        try {
            int row = iCarBrowseService.updateBrowseForDel(appUserId);
            if (row > 0) {
                return JSONResult.ok("清空成功");
            }
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }

    /**
     * @return cn.seocoo.platform.api.util.JSONResult
     * @Author WuJian
     * @Description //单个删除
     * @Date 17:23 2020/1/2
     * @Param [appUserId]
     **/
    @GetMapping("/clearBrowse")
    public JSONResult clearBrowse(Integer id) {
        try {
            int row = iCarBrowseService.clearBrowse(id);
            if (row > 0) {
                return JSONResult.ok(ClassContants.DELETE_MSG);
            }
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }
    }
}
